<?php
/**
 * Created by ThinkPHP5.0.22.
 * User: 小涛.
 * Date: 2018/12/28.
 * Time: 23:37.
 */

namespace app\admin\controller;


use app\admin\model\ShopAddress as ShopAddressModel;
use think\Request;
use app\admin\model\City;

/**
 * Class ShopAddress  发货地址设置
 * @package app\admin\controller
 */
class ShopAddress extends Admin
{
    //列表
    public function index(){
        $list = ShopAddressModel::listShopAddress();
        $this->assign("list",$list);
        return $this->fetch();
    }

    //添加
    public function add(){
        $request = Request::instance();
        if ($request -> isPost()) {
            if ($this->verify_authority() != "") {
                return $this->verify_authority();
            }
            $data = $request -> param();
            $data["create_time"] = time();
            $data["region"] = City::getName($data["city_1"])." ". City::getName($data["city_2"])." ". City::getName($data["city_3"])." ";
            if (!ShopAddressModel::IsShopAddress($data)) {
                $data["update_time"] = time();
                $shop_address = new ShopAddressModel($data);
                if ($shop_address -> save()) {
                    return "添加成功";
                }
            }
            return "添加失败";
        }
        $this -> assign("city_1",City::CityByTid());
        return $this -> fetch();
    }

    //编辑
    public function edit() {
        $request = Request::instance();
        $data = $request -> param();
        $id = 0;
        if (array_key_exists("id",$data)) {
            $id = $data["id"];
        }
        $list = ShopAddressModel::get($id);
        if ($request -> isPost()) {
            if ($this->verify_authority() != "") {
                return $this->verify_authority();
            }
            if ($list) {
                $data["region"] = City::getName($data["city_1"])." ". City::getName($data["city_2"])." ". City::getName($data["city_3"])." ";
                $data["update_time"] = time();
                $shop_address = new ShopAddressModel();
                if ($shop_address -> allowField(true) -> save($data,["id" => $id])) {
                    return "编辑成功";
                }
            }
            return "编辑失败";
        }
        $this -> assign("list",$list);
        $this -> assign("city_1",City::CityByTid());
        return $this -> fetch();
    }

    //删除
    public function Delete(){
        $request = Request::instance();
        if($request -> isPost()){
            if($this->verify_authority() != ""){
                return $this->verify_authority();
            }
            $data = $request -> param();
            $id = 0;
            if(array_key_exists("id",$data)){
                $id = $data["id"];
            }
            $delete = ShopAddressModel::get($id);
            if($delete -> delete()){
                return "删除成功";
            }
            return "删除失败";
        }
    }

}